﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WindowsPhoneApplication3
{
    public partial class Lector : PhoneApplicationPage
    {
        public MainPage pagina1;
        public List<string> ltext;
		public int npagina;
		public int totpaginas = 0;
        public Lector()
        {
            InitializeComponent();
			pagina1 = new MainPage();
            ltext = new List<string>();
			npagina = 2; 
			bmuestra.IsEnabled = false;
        }

        private void blector_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Código obtenido de http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714087(v=vs.105).aspx
        	// Obtain a virtual store for the application.
            System.IO.IsolatedStorage.IsolatedStorageFile local = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            string fecha = pagina1.fecha.Day.ToString() + "-" + pagina1.fecha.Month.ToString() + "-" + pagina1.fecha.Year.ToString() + ".txt";
            // Specify the file path and options.
            try
            {
                using (var isoFileStream =
                        new System.IO.IsolatedStorage.IsolatedStorageFileStream
                            ("DataFolder\\" + fecha, System.IO.FileMode.Open, local))
                {
                    // Read the data.
                    using (var isoFileReader = new System.IO.StreamReader(isoFileStream))
                    {
                        string linea;
                        while ((linea = isoFileReader.ReadLine()) != null)
                        {
                            ltext.Add(linea);
                        }
                    }
                }
				
                string [] palabras = ltext[1].Split(';');
			    tlector.Text = "Código Variedad" + "   " + palabras[0] +"\r\n";
                tlector.Text += "Repetición" + "   " + palabras[1] + "\r\n";
				tlector.Text += "Ensayo" + "   " + palabras[2] +"\r\n";
                tlector.Text += "Fecha Siembra" + "   " + palabras[3] + "\r\n";
                tlector.Text += "Fecha Emergencia" + "   " + palabras[4] + "\r\n";
                tlector.Text += "Fecha Espigadura" + "   " + palabras[5] + "\r\n";
                tlector.Text += "Fecha Floración" + "   " + palabras[6] + "\r\n";
                tlector.Text += "Fecha Madurez" + "   " + palabras[7] + "\r\n";
                tlector.Text += "FS" + "   " + palabras[8] + "\r\n";
                tlector.Text += "DS" + "   " + palabras[9] + "\r\n";
                tlector.Text += "Bolsa" + "   " + palabras[10] + "\r\n";
				tlector.Text += "FB" + "   " +  palabras[11] +"\r\n";
				tlector.Text += "TG" + "   " + palabras[12] +"\r\n";
				tlector.Text += "WG" + "   " + palabras[13] +"\r\n";
				tlector.Text += "DG" + "   " + palabras[14] +"\r\n";
				tlector.Text += "HA" + "   " + palabras[15] +"\r\n";
				tlector.Text += "GLAUCO" + "   " + palabras[16] +"\r\n";
				tlector.Text += "AP" + "   " + palabras[17] +"\r\n";
				tlector.Text += "AT" + "   " + palabras[18] +"\r\n";
				tlector.Text += "LE" + "   " + palabras[19] +"\r\n";
				tlector.Text += "PM" + "   " + palabras[20] +"\r\n";
				tlector.Text += "VI" + "   " + palabras[21] +"\r\n";
				tlector.Text += "AH1" + "   " + palabras[22] +"\r\n";
				tlector.Text += "LH1" + "   " + palabras[23] +"\r\n";
				tlector.Text += "AH2" + "   " + palabras[24] +"\r\n";
				tlector.Text += "LH2" + "   " + palabras[25] +"\r\n";
				tlector.Text += "AH3" + "   " + palabras[26] +"\r\n";
				tlector.Text += "LH3" + "   " + palabras[27] +"\r\n";
				tlector.Text += "MJE" + "   " + palabras[28] +"\r\n";
				tlector.Text += "COBERTURA" + "   " +palabras[29] +"\r\n";
				
				totpaginas = ltext.Count();
				if(totpaginas > 2 ) bmuestra.IsEnabled = true; // Activa el botón para continuar a la página siguiente, si esque el número de paginas de datos es mayor que uno 
				blector.IsEnabled = false;			
            }
            catch 
            {
                MessageBox.Show("No se han creado archivos en el día de hoy");
            }
        }

        private void Flecha1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        { 	
            NavigationService.GoBack();
        }

        private void bmuestra_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	if (npagina < totpaginas)
			{
				string [] palabras = ltext[npagina].Split(';');
				tlector.Text = "Código Variedad" + "   " + palabras[0] +"\r\n";
                tlector.Text += "Repetición" + "   " + palabras[1] + "\r\n";
                tlector.Text += "Ensayo" + "   " + palabras[2] +"\r\n";
                tlector.Text += "Fecha Siembra" + "   " + palabras[3] + "\r\n";
                tlector.Text += "Fecha Emergencia" + "   " + palabras[4] + "\r\n";
                tlector.Text += "Fecha Espigadura" + "   " + palabras[5] + "\r\n";
                tlector.Text += "Fecha Floración" + "   " + palabras[6] + "\r\n";
                tlector.Text += "Fecha Madurez" + "   " + palabras[7] + "\r\n";
                tlector.Text += "FS" + "   " + palabras[8] + "\r\n";
                tlector.Text += "DS" + "   " + palabras[9] + "\r\n";
                tlector.Text += "Bolsa" + "   " + palabras[10] + "\r\n";
				tlector.Text += "FB" + "   " +  palabras[11] +"\r\n";
				tlector.Text += "TG" + "   " + palabras[12] +"\r\n";
				tlector.Text += "WG" + "   " + palabras[13] +"\r\n";
				tlector.Text += "DG" + "   " + palabras[14] +"\r\n";
				tlector.Text += "HA" + "   " + palabras[15] +"\r\n";
				tlector.Text += "GLAUCO" + "   " + palabras[16] +"\r\n";
				tlector.Text += "AP" + "   " + palabras[17] +"\r\n";
				tlector.Text += "AT" + "   " + palabras[18] +"\r\n";
				tlector.Text += "LE" + "   " + palabras[19] +"\r\n";
				tlector.Text += "PM" + "   " + palabras[20] +"\r\n";
				tlector.Text += "VI" + "   " + palabras[21] +"\r\n";
				tlector.Text += "AH1" + "   " + palabras[22] +"\r\n";
				tlector.Text += "LH1" + "   " + palabras[23] +"\r\n";
				tlector.Text += "AH2" + "   " + palabras[24] +"\r\n";
				tlector.Text += "LH2" + "   " + palabras[25] +"\r\n";
				tlector.Text += "AH3" + "   " + palabras[26] +"\r\n";
				tlector.Text += "LH3" + "   " + palabras[27] +"\r\n";
				tlector.Text += "MJE" + "   " + palabras[28] +"\r\n";
				tlector.Text += "COBERTURA" + "   " +palabras[29] +"\r\n";
				++npagina;
				if (npagina >= totpaginas)bmuestra.IsEnabled = false; // Si ya no quedan más paginas a continuación el botón se bloquea
			}
        }
		
		

       
    }
}
